package com.jzr.bedside.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.bedside.db1.model.TMedia;
import com.jzr.bedside.db1.service.TMediaService;
import com.jzr.bedside.db1.vo.MediaVo;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.common.util.UrlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Api("多媒体接口")
@RestController
@RequestMapping("/api/media")
public class MediaController {
    @Value("${file.media-download-url}")
    private String downloadUrl;

    @Autowired
    private TMediaService mediaService;

    @ApiOperation(("获取多媒体列表"))
    @GetMapping("/get")
    public Result<List<MediaVo>> get(@ApiParam("多媒体查询参数") TMedia record){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

        QueryWrapper<TMedia> qw = new QueryWrapper<>();
        qw.orderByAsc("sort");
        qw.isNotNull(TMedia.FileName_);
        qw.eq(TMedia.Status_,1);
        if(record != null){
            if(U.isNotBlank(record.getDeptCode())){
                qw.eq(TMedia.DeptCode_,record.getDeptCode());
            }
            if(U.isNotBlank(record.getFunType())){
                qw.eq(TMedia.FunType_,record.getFunType());
            }
        }

        List<MediaVo> listResult = new ArrayList<>();
        List<TMedia> list = mediaService.list(qw);
        if(list != null){
            for(TMedia m : list){
                MediaVo v = new MediaVo();
                BeanUtils.copyProperties(m,v);
                v.setUrl(UrlUtil.concat(downloadUrl,m.getFileName())+"?v="+m.getUpdateTime().format(dateTimeFormatter));
                listResult.add(v);
            }
        }
        return Result.success("成功",listResult);
    }
}
